package com.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import java.util.List;

/**
 * Created by Administrator on 2017/7/6.
 */
public class HelloWorld extends BasicTestCase{

    @Test
    public void testName() throws Exception {

       /* Thread.sleep(10000);*/
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.netease.newsreader.activity:id/bfw")));
//        driver.findElement(By.id("com.netease.newsreader.activity:id/bfw")).click();
//        WebElement elementET = driver.findElement(By.id("com.netease.newsreader.activity:id/ago"));
/*        elementET.sendKeys("hello world");
        Thread.sleep(3000);*/
/*
        elementET.sendKeys("中国你好！");
        Thread.sleep(3000);
        elementET.clear();
        Thread.sleep(3000);
*/
/*        //点击 网易新闻搜索按钮
        //by id 定位
        WebElement element = driver.findElement(By.id("com.netease.newsreader.activity:id/bf2"));
        element.click();*/

/*        //by xpath 定位
        WebElement element1 = driver.findElement(By.xpath("//android.widget.TextView[@text='娱乐']"));
        element1.click();*/

       /* //by class 定位
        WebElement element2 =  driver.findElements(By.className("android.widget.ImageView")).get(1);
        element2.click();
        Thread.sleep(4000);*/
/*
       //长按
        //点击  我的
        WebElement myTab = driver.findElements(By.id("com.netease.newsreader.activity:id/a04")).get(4);
        myTab.click();
        // 点击 收藏
        driver.findElement(By.id("com.netease.newsreader.activity:id/a4b")).click();
        //长按 收藏的标题
        WebElement firstItem = driver.findElements(By.id("com.netease.newsreader.activity:id/dz")).get(0);
        TouchAction action = new TouchAction(driver);
        // 长按
        action.longPress(firstItem).perform().release();
        Thread.sleep(4000);
*/
        //通过findElements  找到当前新闻的标题id 所有新闻的集合
        List<WebElement> elements = driver.findElements(By.id("com.netease.newsreader.activity:id/dz"));
        String expectedText = "广河高速一载44人大巴翻车致19死 伤员已全部救出";
        int index = -1;
        Thread.sleep(1000);
        //遍历 这个集合
        for (int i=0;i < elements.size();i++){
            String actual = elements.get(i).getText().toString();
            if (elements.get(i).getText().contains(expectedText)){
                index = i;
                break;
            }
            System.out.println("actual : "+actual);
            System.out.println("i : "+i);
            System.out.println("index : "+index);
        }
        System.out.println("========== "+ index);
    }

}
